Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur.
SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer
Ravi Vishwakarma
17-Jun-2024A closure in JavaScript is a feature where an inner function has access to its outer (enclosing) function's variables even after the outer function has finished executing. Closures are created whenever a function is created, allowing the function to remember the scope in which it was created.
Key Characteristics of Closures:
Example of a Closure:
In this example:
outerFunctiondeclares a variableouterVariableand defines aninnerFunction.innerFunctionhas access toouterVariablebecause it is within the scope ofouterFunction.outerFunctionis called, it returnsinnerFunction. TheinnerFunctionmaintains access toouterVariableeven afterouterFunctionhas completed execution.closure()(which is the returnedinnerFunction) logs the value ofouterVariable.Practical Example :
Output: